package com.project.zsxd.server.clocksleep.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.zsxd.server.clocksleep.entity.ClockSleep;
import com.project.zsxd.server.clocksleep.service.IClockSleepService;
import com.project.zsxd.server.common.bean.Result;
import com.project.zsxd.server.user.entity.User;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;
import java.util.Date;

/**
 * <p>
 * 睡眠打卡表 前端控制器
 * </p>
 *
 * @author kaler
 * @since 2020-09-23
 */
@RestController
@RequestMapping("/clockSleep")
public class ClockSleepController {

    @Autowired
    private IClockSleepService clockSleepService;

    /**
     * 开始入睡
     * @return
     */
    @RequestMapping("startSleep")
    public Result startSleep() {
        User currentUser = (User) SecurityUtils.getSubject().getPrincipal();
        ClockSleep clockSleep = new ClockSleep();
        clockSleep.setSleepTime(new Date());
        clockSleep.setUserName(currentUser.getUserName());
        clockSleepService.save(clockSleep);
        return Result.success(null);
    }

    /**
     * 查询当天是否已入睡
     * 当天18点至次日6点入睡过都算当日已入睡
     * @return
     */
    @RequestMapping("queryIsSleepping")
    public Result queryIsSleepping() {
        User currentUser = (User) SecurityUtils.getSubject().getPrincipal();
        Calendar startCal = Calendar.getInstance();
        startCal.set(Calendar.HOUR_OF_DAY, 18);
        startCal.set(Calendar.MINUTE, 0);
        startCal.set(Calendar.SECOND, 0);
        startCal.set(Calendar.MILLISECOND, 0);

        Calendar endCal = Calendar.getInstance();
        endCal.add(Calendar.DAY_OF_MONTH, 1);
        endCal.set(Calendar.HOUR_OF_DAY, 6);
        endCal.set(Calendar.MINUTE, 0);
        endCal.set(Calendar.SECOND, 0);
        endCal.set(Calendar.MILLISECOND, 0);

        QueryWrapper<ClockSleep> queryWrapper = new QueryWrapper<ClockSleep>().eq("user_name", currentUser.getUserName())
                .ge("sleep_time", startCal.getTime())
                .lt("sleep_time", endCal.getTime());
        int count = clockSleepService.count(queryWrapper);
        return Result.success(count > 0);
    }

    /**
     * 查询最近30天的入睡时间
     * @return
     */
    @RequestMapping("queryLastMonthSleepTimes")
    public Result queryLastMonthSleepTimes() {
        User currentUser = (User) SecurityUtils.getSubject().getPrincipal();
        QueryWrapper<ClockSleep> queryWrapper = new QueryWrapper<ClockSleep>().eq("user_name", currentUser.getUserName())
                .orderByDesc("sleep_time");
        IPage<ClockSleep> page = new Page<>();
        page.setSize(30);
        page = clockSleepService.page(page, queryWrapper);
        return Result.success(page);
    }


}
